package com.bobo.rbac.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bobo.rbac.auth.utils.ResponseResult;
import com.bobo.rbac.domain.Role;
import com.bobo.rbac.domain.UserRole;
import com.bobo.rbac.service.RoleMenuService;
import com.bobo.rbac.service.RoleService;
import com.bobo.rbac.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: BOBO
 * @time: 2022/7/28 22:42
 */

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    RoleMenuService roleMenuService;

    @RequestMapping("list")
    public ResponseResult list(){
        List<Role> list = roleService.list();
        return ResponseResult.ok(list);
    }

    @GetMapping("getRoleIdsByUserId")
    public ResponseResult getRoleIdsByUserId(Integer uid){
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        List<UserRole> list = userRoleService.list(wrapper);
        //获取用户的角色id集合
        List<Integer> roleIdList = list.stream().map(ur -> ur.getRid()).collect(Collectors.toList());
        return ResponseResult.ok(roleIdList);
    }

    /**
     * 绑定角色菜单
     * @return
     */
    @RequestMapping("bindMenu")
    public ResponseResult bindMenu(@RequestBody  Role role){
        boolean b= roleMenuService.bindMenu(role);
        return ResponseResult.ok();
    }


    /**
     * 统计角色的人数
     * @return
     */
    @RequestMapping("selectCountByPid")
    public ResponseResult selectCountByPid(){
        List<Role> roles = roleService.selectCountByRid();
        return ResponseResult.ok(roles);

    }

}
